﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 课程设计
{
    public class UserBll
    {
        public static bool LogIn(User user)
        {
            if (UserDal.SelectCount(user) == 1)
            {
                user.HasLoggedIn = true;
                user.Message = "登录成功。";
            }
            else
            {
                user.HasLoggedIn = false;
                user.Message = "用户号/密码有误，请重新输入！";
            }
            return user.HasLoggedIn;
        }
        public static bool SignUp(User user)
        {
            int rowAffected1 = UserDal.Insert(user);
            if (rowAffected1 == 1)
            {
                user.HasSignedUp = true;
                user.Message = "注册成功。";
            }
            else
            {
                user.HasSignedUp = false;
                if (user.IsDuplicate)
                {
                    user.Message = "您注册的用户号已存在，请重新输入！\n";
                }
                user.Message += "注册失败！";
            }
            return user.HasSignedUp;
        }
        public static bool AlterPassword(User user,string newPassword, string newPassword2)
        {
            bool isSuccess = false;
            if (UserDal.SelectCount(user) == 1)
            {
                if (newPassword == newPassword2)
                {
                    user.Password = newPassword;
                    isSuccess = (UserDal.AlterPassword(user) == 1);
                    if (isSuccess)
                    {
                        user.Message = "密码更新成功。";
                    }
                    else
                    {
                        user.Message = "密码更新失败！";
                    }
                }
                else
                {
                    user.Message = "两次输入的新密码不一致！";
                }
            }
            else
            {
                user.Message = "原密码错误，请输入正确的密码！";
            }
            return isSuccess;
        }
    }
}
